﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using System.Xml.Linq;

/// <summary>
/// Summary description for Logger
/// </summary>
public class Facebook
{

    public Facebook()
    {

    }
    private static string HttpPost(string pUrl, string pPostData)
    {
        HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(pUrl);
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";
        byte[] bytes = System.Text.Encoding.UTF8.GetBytes(pPostData);
        Stream requestWriter = webRequest.GetRequestStream(); //GetRequestStream
        requestWriter.Write(bytes, 0, bytes.Length);
        requestWriter.Close();

        Stream responseStream = null;
        StreamReader responseReader = null;
        string responseData = "";
        try
        {
            WebResponse webResponse = webRequest.GetResponse();
            responseStream = webResponse.GetResponseStream();
            responseReader = new StreamReader(responseStream);
            responseData = responseReader.ReadToEnd();
        }
        catch (Exception exc)
        {
            throw new Exception("could not post : " + exc.Message);
        }
        finally
        {
            if (responseStream != null)
            {
                responseStream.Close();
                responseReader.Close();
            }
        }

        return responseData;
    }

    public static bool WriteOnFace(string message, string accessToken, string picture, string link)
    {
        return false;
        try
        {
            //In this function we will write to the users Facebook wall by posting the message through the Facebook Graph API

            //The post url need to be appended :
            //1) A username (I don't understand why, however I guess "me" will translate to the user embedded in the access_token)
            //2) A data type (here feed)
            //3) The access_token

            string username = "me";
            string datatype = "feed";
            string urlWriteWall = "https://graph.facebook.com/" + username + "/" + datatype + "?access_token=" + accessToken;

            //the message to post as a key/value pair
            string entityMessage = "message=" + message;
            if (string.IsNullOrEmpty(picture))
            {
                picture = "http://revio.ge/upload/no-company-logo.jpg";
            }
            entityMessage += "&picture=" + picture;
            entityMessage += "&link=" + link;

            HttpPost(urlWriteWall, entityMessage); //we write HttpPost a little later

            //string urlll = "";
            //WebClient wc = new WebClient();
            //urlll = "https://graph.facebook.com/feed?access_token=" + accessToken + "&message=" + message + "&method=post";
            //wc.DownloadString(urlll);

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }

    public static void BanUser(string fbUserId)
    {
        var bannedUsersXml = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/BannedUsers.xml");
        bannedUsersXml.Add(new XElement("user") { Value = fbUserId });
        bannedUsersXml.Save(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/BannedUsers.xml");
    }
}